<template>
	<div>
		<div class="tip-text">注意事项：平台至多保存一周（7天）的电桩日志</div>
		<Table ref="tableRef" :columns="data.columns" :toolbar="data.toolbar" :handles="data.handles"
			:query="data.query" @get-list="getData"></Table>
	</div>
</template>

<script lang="ts" setup name="stationList">
	import Table from "@/components/table/index.vue";
	import {
		pagingConnectorMessage,
		EXPORT_CONNECTORMESSAGE,
	} from "@/service/apis/logs";
	import { exportFile } from "@/utils/utils";

	const router = useRouter();

	const tableRef = ref();

	const DATATYPE = ([
		{ label: "1-电桩登录", value: "电桩登录" },
		{ label: "2-电桩登录回复", value: "电桩登录回复" },
		{ label: "3-心跳", value: "心跳" },
		{ label: "4-心跳回复", value: "心跳回复" },
		{ label: "5-计费模型验证", value: "计费模型验证" },
		{ label: "6-计费模型验证回复", value: "计费模型验证回复" },
		{ label: "7-充电桩计费模型请求", value: "充电桩计费模型请求" },
		{ label: "8-充电桩计费模型请求应答", value: "充电桩计费模型请求应答" },
		{ label: "9-充电桩计费模型请求", value: "充电桩计费模型请求" },
		{ label: "10-充电桩计费模型请求应答", value: "充电桩计费模型请求应答" },
		{ label: "19-实时监测数据（旧）", value: "实时监测数据（旧）" },
		{ label: "17-实时监测数据", value: "实时监测数据" },
		{ label: "21-充电握手", value: "充电握手" },
		{ label: "23-参数配置", value: "参数配置" },
		{ label: "25-充电结束阶段", value: "充电结束阶段" },
		{ label: "27-错误报文", value: "错误报文" },
		{ label: "29-充电阶段BMS中止", value: "充电阶段BMS中止" },
		{ label: "33-充电阶段充电机中止", value: "充电阶段充电机中止" },
		{ label: "35-充电过程BMS需求与充电机输出", value: "充电过程BMS需求与充电机输出" },
		{ label: "37-充电过程BMS信息", value: "充电过程BMS信息" },
		{ label: "39-实时监测数据（三相）", value: "实时监测数据（三相）" },
		{ label: "49-充电桩主动申请启动充电", value: "充电桩主动申请启动充电" },
		{ label: "50-运营平台确认启动充电", value: "运营平台确认启动充电" },
		{ label: "51-远程启动充电命令回复", value: "远程启动充电命令回复" },
		{ label: "52-远程启动充电命令", value: "远程启动充电命令" },
		{ label: "54-运营平台远程停机", value: "运营平台远程停机" },
		{ label: "53-远程停机命令回复", value: "远程停机命令回复" },
		{ label: "55-0x37交易记录-v1.1", value: "0x37交易记录-v1.1" },
		{ label: "56-交易记录应答-v1.1", value: "交易记录应答-v1.1" },
		{ label: "59-0x3B交易记录", value: "0x3B交易记录" },
		{ label: "63-0x3F交易记录", value: "0x3F交易记录" },
		{ label: "64-交易记录应答", value: "交易记录应答" },
		{ label: "65-余额更新应答", value: "余额更新应答" },
		{ label: "66-余额更新设置", value: "余额更新设置" },
		{ label: "78-固件版本应答", value: "固件版本应答" },
		{ label: "79-固件版本上传", value: "固件版本上传" },
		{ label: "83-参数查询回复", value: "参数查询回复" },
		{ label: "84-参数查询", value: "参数查询" },
		{ label: "85-对时设置应答", value: "对时设置应答" },
		{ label: "86-对时设置设置", value: "对时设置设置" },
		{ label: "87-计费模型应答", value: "计费模型应答" },
		{ label: "88-计费模型设置", value: "计费模型设置" },
		{ label: "89-二维码设置应答", value: "二维码设置应答" },
		{ label: "90-二维码设置", value: "二维码设置" },
		{ label: "91-模块实时监测数据", value: "模块实时监测数据" },
		{ label: "93-充电桩故障上传", value: "充电桩故障上传" },
		{ label: "101-参数设置回复", value: "参数设置回复" },
		{ label: "102-参数设置", value: "参数设置" },
		{ label: "113-充电桩功率调控参数上行", value: "充电桩功率调控参数上行" },
		{ label: "114-充电桩功率调控参数应答", value: "充电桩功率调控参数应答" },
		{ label: "115-充电桩功率调控设置应答", value: "充电桩功率调控设置应答" },
		{ label: "116-充电桩功率调控设置", value: "充电桩功率调控设置" },
		{ label: "117-监测数据", value: "监测数据" },
		{ label: "118-召唤监测数据", value: "召唤监测数据" },
		{ label: "145-重启命令回复", value: "重启命令回复" },
		{ label: "146-重启命令", value: "重启命令" },
		{ label: "147-远程更新回复", value: "远程更新回复" },
		{ label: "148-远程更新", value: "远程更新" },
	]);

	const emit = defineEmits(["get-update"]);
	onActivated(() => {
		emit("get-update", (flag : boolean) => {
			if (flag) {
				tableRef.value?.getData();
			}
		});
	});

	const data = reactive({
		columns: [
			{
				prop: "stationName",
				label: "归属电站",
			},
			{
				prop: "equipmentCode",
				label: "电桩编码",
			},
			{
				prop: "connectorCode",
				label: "终端编码",
			},
			{
				prop: "type",
				label: "设备报文类型",
			},
			{
				prop: "originalMsg",
				label: "原始报文",
			},
			{
				prop: "createTime",
				label: "上送时间",
			},
		],
		toolbar: [
			{
				label: "导出",
				permission: "pileMessageList.export",
				icon: "icon-export",
				type: "primary",
				plain: true,
				handle: (row : any) => {
					const form = tableRef.value?.getForm();
					if (form?.time?.length > 0) {
						form.startTime = form.time[0];
						form.endTime = form.time[1];
					}
					exportFile(EXPORT_CONNECTORMESSAGE, '电桩报文', form);
				},
			},
		],
		handles: [
			{
				label: "详情",
				handle: (row : any) => {
					router.push({
						name: "pileMessageDetail",
						query: {
							id: row.id,
							originalMsg: row.originalMsg,
							createTime: row.createTime,
							type: row.type,
							equipmentCode: row.equipmentCode,
							detail: row.detail,
						},
					});
				},
			},
		],
		query: [
			{
				label: "终端编码",
				prop: "connectorCode",
				comName: "input",
				props: {
					placeholder: "请输入终端编码",
				},
			},
			{
				label: "电桩编码",
				prop: "equipmentCode",
				comName: "input",
				props: {
					placeholder: "请输入电桩编码",
				},
			},
			{
				label: "设备报文类型",
				prop: "type",
				comName: "select",
				props: {
					placeholder: "请选择设备报文类型",
					options: DATATYPE,
				},
			},
			{
				label: "上送时间",
				prop: "time",
				comName: "date",
				keys: ["startTime", "endTime"],
				props: {
					// type: "daterange",
					type: "datetimerange",
				},
			},
			{
				label: "归属电站",
				prop: "stationName",
				comName: "input",
				props: {
					placeholder: "请输入归属电站",
				},
			},
		],
	});

	const getData = (page : any, search : any) => {
		const form : any = search.ruleForm || {};
		if (form?.time?.length > 0) {
			form.startTime = form.time[0];
			form.endTime = form.time[1];
		} else {
			form.startTime = null;
			form.endTime = null;
		}
		pagingConnectorMessage(form, page).then((res : any) => {
			if (res) {
				if (page.pageNo !== 1 && (!res.dataList || res.dataList?.length === 0)) {
					page.pageNo--;
					getData(page, search);
					return;
				}
				nextTick(() => {
					tableRef.value?.setTable({
						total: res.totalCount,
						table: res.dataList,
					});
				});
			}
		});
	};
</script>
<style lang="scss" scoped>
	.tip-text {
		font-size: #333333;
		font-size: 14px;
		font-weight: 400;
		padding: 12px 24px 12px 42px;
		background-color: rgb(198, 226, 255);
		border-radius: 12px;
	}
</style>